package com.xing.netty;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

/**
 * Author: linjx
 * Date: 2018/11/30
 */
public class NExceptionExample {
    public static void main(String[] args) throws Exception{
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup wokerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, wokerGroup)
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG, 128)
                    .option(ChannelOption.TCP_NODELAY, true)
                    .handler(new LoggingHandler(LogLevel.DEBUG))
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) {
                            socketChannel.pipeline().addLast(new InBoundB());
                            socketChannel.pipeline().addLast(new InBoundA());
                            socketChannel.pipeline().addLast(new InBoundC());
                            socketChannel.pipeline().addLast(new OutBoundA());
                            socketChannel.pipeline().addLast(new OutBoundB());
                            socketChannel.pipeline().addLast(new OutBoundC());
                        }
                    });

            ChannelFuture f = b.bind(8888).sync();
            f.channel().closeFuture().sync();
        } finally {
            bossGroup.shutdownGracefully();
            wokerGroup.shutdownGracefully();
        }
    }

    public static class InBoundA extends ChannelInboundHandlerAdapter {
        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            System.out.println("InA.ex");
            ctx.fireExceptionCaught(cause);
        }
    }

    public static class InBoundB extends ChannelInboundHandlerAdapter {
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            throw new BusinessException("from InB");
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            System.out.println("InB.ex");
            ctx.fireExceptionCaught(cause);
        }
    }

    public static class InBoundC extends ChannelInboundHandlerAdapter {
        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            System.out.println("InC.ex");
            ctx.fireExceptionCaught(cause);
        }
    }

    public static class OutBoundA extends ChannelOutboundHandlerAdapter {
        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            System.out.println("OutA.ex");
            ctx.fireExceptionCaught(cause);
        }
    }

    public static class OutBoundB extends ChannelOutboundHandlerAdapter {
        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            System.out.println("OutB.ex");
            ctx.fireExceptionCaught(cause);
        }
    }

    public static class OutBoundC extends ChannelOutboundHandlerAdapter {
        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            System.out.println("OutC.ex");
            ctx.fireExceptionCaught(cause);
        }
    }

    public static class BusinessException extends Exception{
        public BusinessException(String s) {
            super(s);
        }
    }

    public static class ExceptionHandler extends ChannelInboundHandlerAdapter {
        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            if (cause instanceof BusinessException) {
                System.out.println("BusinessException");
            }
        }
    }

}
